var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var base64 = require('../../utils/Base64.js');
var app = getApp();

var uid = 0;
var token = '';

var wxAuthFail = true;
var inviteuid = '';


Page({

  /**
   * 页面的初始数据
   */
  data: {


    uid: 0,
    token: '',
    openid: '',



  },

  titleName: '',
  web_view_url: '',

  isOnLoad: true,

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(data) {
    console.log("data==", data);
    var that = this;
    this.web_view_url = base64.decode(data.weburl);
    console.log("网页地址：" + this.web_view_url);

    this.titleName = data.title ? data.title : '直播';
    wx.setNavigationBarTitle({
      title: that.titleName,
    })
    
    inviteuid = data.inviteuid ? data.inviteuid : '';

    this.isOnLoad = true;
    that.checkAuthInfo(inviteuid);

  },

  checkAuthInfo: function(inviteuid) {
    var that = this;
    wx.getSetting({
      success: (res) => {
        console.log("已授权列表：", res);
        var authStatus_userInfo = res.authSetting["scope.userInfo"];
        if (authStatus_userInfo) {
          that.getLoginInfo(inviteuid);
        } else {
          wx.navigateTo({
            url: "/pages/authorization/authorization",
          })
        }
      }
    });
  },

  getLoginInfo: function(inviteuid) {
    var that = this;
    app.LoginUserInfo(function(res) {
      console.log("mine login:", res);
      wxAuthFail = res == -1; //用户拒绝授权
      if (wxAuthFail) {
        return;
      }
      that.setData({
        uid: res.uid,
        token: res.token,
        url: that.web_view_url + '&uid=' + res.uid
      });
      console.log(res.uid);
      var id = inviteuid ? inviteuid : res.uid;
      util.api({
        data: {
          "uid": id,
          "publickey": util.publiccode,
          "ac": "userinfo"
        },
        success: function(responseData) {
          console.log("用户信息", responseData.data);
          that.setData({
            userinfo: responseData.data
          });
        }
      });

    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (!this.isOnLoad) {
      var that = this;
      wx.getSetting({
        success: (res) => {
          console.log("已授权列表：", res);
          var authStatus_userInfo = res.authSetting["scope.userInfo"];
          if (authStatus_userInfo) {
            that.getLoginInfo(inviteuid);
          } else {
            if(getCurrentPages().length > 1) {
              wx.navigateBack({
                delta:1
              });
            } else {
              wx.redirectTo({
                url: '/pages/live_player_list/live_player_list',
              });
            } 
          }
        }
      });
    }
    this.isOnLoad = false;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  //页面分享
  onShareAppMessage: function(res) {

    var url = this.web_view_url;
    var url_base64 = base64.encode(url);
    var path = "/pages/zhibo/zhibo?title=" + this.titleName + "&weburl=" + url_base64;

    return {
      title: '直播分享',
      path: path
    };
  },


})